<template>
    <div>
      <ly-dialog
        v-model="dialogVisible"
        :title="loadingTitle"
        width="560px"
        :before-close="handleClose">
        <el-form
          :inline="false"
          :model="formData"
          :rules="rules"
          ref="rulesForm"
          label-position="right"
          label-width="130px">
          
  
          <el-form-item label="学校人数：" prop="school_num">
            <el-input
              v-model.trim="formData.school_num"
              placeholder="请输入学校人数"
              style="width: 300px"
              :disabled="loadingTitle == '详情'"></el-input>
          </el-form-item>
  
          <el-form-item label="班级个数：" prop="class_num">
            <el-input
              v-model.trim="formData.class_num"
              placeholder="请输入班级个数"
              style="width: 300px"
              :disabled="loadingTitle == '详情'"></el-input>
          </el-form-item>
  
          <el-form-item label="教师个数：" prop="teacher_num">
            <el-input
              v-model.trim="formData.teacher_num"
              placeholder="请输入教师个数"
              style="width: 300px"
              :disabled="loadingTitle == '详情'"></el-input>
          </el-form-item>
  
          <el-form-item label="学校负责人姓名" prop="director_name">
            <el-input
              v-model.trim="formData.director_name"
              placeholder="请输入学校该项目负责人姓名"
              style="width: 300px"
              :disabled="loadingTitle == '详情'"></el-input>
          </el-form-item>
  
          <el-form-item label="学校负责人电话" prop="director_phone">
            <el-input
              v-model.trim="formData.director_phone"
              placeholder="请输入学校该项目负责人电话"
              style="width: 300px"
              :disabled="loadingTitle == '详情'"></el-input>
          </el-form-item>
  
          <el-form-item label="职务" prop="job">
            <el-input
              v-model.trim="formData.job"
              placeholder="职务"
              style="width: 300px"
              :disabled="loadingTitle == '详情'"></el-input>
          </el-form-item>
  
          <el-form-item label="在地工作人员名称" prop="user_name">
            <el-select
              v-model="formData.user_name"
              placeholder="请选择在地工作人员名称"
              clearable
              style="width: 300px"
              @change="search(1, $event)">
              <el-option
                v-for="item in userIdList"
                :key="item.id"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
  
          <el-form-item label="入校计划" prop="status">
            <el-radio-group v-model="formData.status" @change="getData">
              <el-radio :label="1">待制定</el-radio>
              <el-radio :label="2">已制定</el-radio>
            </el-radio-group>
          </el-form-item>
  
          <!-- <el-form-item label="所属学校：" prop="school_id">
            <el-select
              v-model="formData.school_id"
              placeholder="所属学校"
              clearable
              style="width: 300px"
              @change="search(2, $event)">
              <el-option
                v-for="item in schoolList"
                :key="item.id"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item> -->
  
          <el-form-item label="是否直播工具：" prop="instrument">
            <el-select
              v-model="formData.instrument"
              placeholder="是否直播工具"
              clearable
              style="width: 300px"
              @change="search(2, $event)">
              <el-option
                v-for="item in linePlanList"
                :key="item.id"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
  
          <el-form-item label="年级：" prop="grade">
            <el-input
              v-model.trim="formData.grade"
              placeholder="请输入年级"
              style="width: 300px"
              :disabled="loadingTitle == '详情'"></el-input>
          </el-form-item>
  
          <el-form-item label="学校班级：" prop="class_name">
            <el-input
              v-model.trim="formData.class_name"
              placeholder="请输入学校班级"
              style="width: 300px"
              :disabled="loadingTitle == '详情'"></el-input>
          </el-form-item>
  
          <el-form-item label="班级课程：" prop="course">
            <el-input
              v-model.trim="formData.course"
              placeholder="请输入班级课程"
              style="width: 300px"
              :disabled="loadingTitle == '详情'"></el-input>
          </el-form-item>
  
          <el-form-item label="第几节：" prop="some">
            <el-input
              v-model.trim="formData.some"
              placeholder="请输入第几节"
              style="width: 300px"
              :disabled="loadingTitle == '详情'"></el-input>
          </el-form-item>
  
          <el-form-item label="周几：" prop="week">
            <el-input
              v-model.trim="formData.week"
              placeholder="请输入周一至周日"
              style="width: 300px"
              :disabled="loadingTitle == '详情'"></el-input>
          </el-form-item>
  
          <el-form-item label="上课时间：" prop="tart_time">
            <el-input
              v-model.trim="formData.tart_time"
              placeholder="请输入上课时间"
              style="width: 300px"
              :disabled="loadingTitle == '详情'"></el-input>
          </el-form-item>
  
          <el-form-item label="下课时间:" prop="end_time">
            <el-input
              v-model.trim="formData.end_time"
              placeholder="请输入下课时间"
              style="width: 300px"
              :disabled="loadingTitle == '详情'"></el-input>
          </el-form-item>
  
          <el-form-item label="实际上课时间:" prop="num_time">
            <el-input
              v-model.trim="formData.num_time"
              placeholder="请输入实际上课时间"
              style="width: 300px"
              :disabled="loadingTitle == '详情'"></el-input>
          </el-form-item>
  
          <el-form-item label="在地老师名称:" prop="zuser_name">
            <el-input
              v-model.trim="formData.zuser_name"
              placeholder="请输入在地老师名称"
              style="width: 300px"
              :disabled="loadingTitle == '详情'"></el-input>
          </el-form-item>
  
          <el-form-item label="联系电话:" prop="user_phone">
            <el-input
              v-model.trim="formData.user_phone"
              placeholder="请输入联系电话"
              style="width: 300px"
              :disabled="loadingTitle == '详情'"></el-input>
          </el-form-item>
  
          <el-form-item label="班级规模:" prop="class_mode">
            <el-input
              v-model.trim="formData.class_mode"
              placeholder="请输入班级规模"
              style="width: 300px"
              :disabled="loadingTitle == '详情'"></el-input>
          </el-form-item>
  
          <!-- <el-form-item label="入校计划审核" prop="state">
            <el-select
              v-model="formData.state"
              placeholder="入校计划审核"
              clearable
              style="width: 300px"
              @change="search(3, $event)">
              <el-option
                v-for="item in inSchoolPlan"
                :key="item.id"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item> -->
  
          <!-- <el-form-item label="是否排课" prop="is_status">
            <el-select
              v-model="formData.is_status"
              placeholder="是否排课"
              clearable
              style="width: 300px"
              @change="search(4, $event)">
              <el-option
                v-for="item in isStatusList"
                :key="item.id"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item> -->
        </el-form>
        <template #footer>
          <el-button @click="handleClose" :loading="loadingSave">取消</el-button>
          <el-button
            type="primary"
            @click="submitData"
            :loading="loadingSave"
            :disabled="loadingTitle == '详情'"
            >确定</el-button
          >
        </template>
      </ly-dialog>
    </div>
  </template>
  
  <script>
  import {
    schoolAdd,
    schoolUpdate,
    addressAreaRoot,
    schoolIdList,
  } from "@/api/api";
  import LyDialog from "@/components/dialog/dialog.vue";
  export default {
    emits: ["refreshData"],
    name: "addAdmin",
    components: { LyDialog },
    data() {
      return {
        dialogVisible: false,
        loadingSave: false,
        loadingTitle: "",
        userIdList: [],
        planList: [
          {
            value: "1",
            label: "待制定",
            id: "1",
          },
          {
            value: "2",
            label: "已制定",
            id: "2",
          },
        ],
        inSchoolPlan: [
          {
            value: "1",
            label: "待审核",
            id: "1",
          },
          {
            value: "2",
            label: "已审核",
            id: "2",
          },
        ],
        isStatusList: [
          {
            value: "1",
            label: "未排课",
            id: "1",
          },
          {
            value: "2",
            label: "已排课",
            id: "2",
          },
        ],
        formData: { 
          school_num: "", // 学校人数
          class_num: "", // 班级个数
          teacher_num: "", // 教师人数
          director_name: "", // 学校该项目负责人姓名
          director_phone: "", // 学校该项目负责人电话
          job: "", // 职务
          user_id: "", // 在地工作人员id
          user_name: "", // 在地工作人员名称
          status: "", // 入校计划:1=待制定,2=已制定
          // school_name: "", // 所属学校
          // school_id: "", // 所属学校id
          instrument: "", // 是否直播工具:1=是,2=否
          grade: "", // 年级
          class_name: "", // 班级
          course: "", // 课程
          some: "", // 第几节
          week: "", // 周几
          tart_time: "", // 上课时间
          end_time: "", // 下课时间
          num_time: "", // 实际上课时间
          zuser_name: "", // 在地老师名称
          user_phone: "", // 联系电话
          class_mode: "", // 班级规模
          state: "", // 入校计划审核 1 2
          is_status: "", // 是否排课  1 2
        },
        rules: {
           
          school_num: [
            { required: true, message: "请输入学校人数", trigger: "blur" },
          ],
          class_num: [
            { required: true, message: "请输入班级个数", trigger: "blur" },
          ],
          teacher_num: [
            { required: true, message: "请输入教师人数", trigger: "blur" },
          ],
          director_name: [
            {
              required: true,
              message: "请输入学校项目负责人姓名",
              trigger: "blur",
            },
          ],
          director_phone: [
            {
              required: true,
              message: "请输入学校项目负责人电话",
              trigger: "blur",
            },
          ],
          job: [{ required: true, message: "请输入职务", trigger: "blur" }],
          user_name: [
            {
              required: true,
              message: "请选择在地工作人员名称",
              trigger: "blur",
            },
          ],
          instrument: [
            { required: true, message: "请选择直播工具", trigger: "blur" },
          ],
          grade: [{ required: true, message: "请输入年级", trigger: "blur" }],
          class_name: [
            { required: true, message: "请输入班级", trigger: "blur" },
          ],
          course: [{ required: true, message: "请输入课程", trigger: "blur" }],
          some: [{ required: true, message: "请输入第几节", trigger: "blur" }],
          week: [{ required: true, message: "请输入周几", trigger: "blur" }],
          tart_time: [
            { required: true, message: "请输入上课时间", trigger: "blur" },
          ],
          end_time: [
            { required: true, message: "请输入下课时间", trigger: "blur" },
          ],
          num_time: [
            { required: true, message: "请输入实际上课时间", trigger: "blur" },
          ],
          zuser_name: [
            { required: true, message: "请输入在地老师名称", trigger: "blur" },
          ],
          user_phone: [
            { required: true, message: "请输入联系电话", trigger: "blur" },
          ],
          class_mode: [
            { required: true, message: "请输入班级规模", trigger: "blur" },
          ],
          state: [
            { required: true, message: "请选择入校计划审核", trigger: "blur" },
          ],
          is_status: [
            { required: true, message: "请选择是否排课", trigger: "blur" },
          ],
        },
        GroupList: [], //管理员分组列表
        AreaRootList: [], //区域列表获取
        passwordSwitch: false, //是否修改密码
        linePlanList: [
          {
            value: "1",
            label: "是",
            id: "1",
          },
          {
            value: "2",
            label: "否",
            id: "2",
          },
        ],
      };
    },
    methods: {
      search(i, e) {
        if (i === 1)
          this.formData.user_id = this.userIdList.find(
            (item) => item.value == e
          ).value;
        if (i === 2)
          this.formData.instrument = this.linePlanList.find(
            (item) => item.value == e
          ).value;
        if (i === 3)
          this.formData.state = this.inSchoolPlan.find(
            (item) => item.value == e
          ).value;
        if (i === 4)
          this.formData.is_status = this.isStatusList.find(
            (item) => item.value == e
          ).value;
      },
      getData(e) {
        this.formData.status = e;
      },
      handleClose() {
        this.dialogVisible = false;
        this.loadingSave = false;
        this.formData = {};
        this.passwordSwitch = false;
        this.$emit("refreshData");
      },
      addModuleFn(item, flag, GroupList) {
        this.loadingTitle = flag;
        this.dialogVisible = true;
        schoolIdList().then((res) => {
          this.userIdList = res.data.map((item) => {
            return {
              value: item.id,
              label: item.username,
            };
          });
        });
        if (!this.GroupList&&!this.GroupList.length) this.GroupList = GroupList;
        if (item) {
          this.formData = item;
          this.formData.status = Number(this.formData.status);
        }
      },
  
      submitDataOne() {
        let vm = this;
  
        this.$refs["rulesForm"].validate((obj) => {
          if (obj) {
            this.loadingSave = true;
  
            if (vm.passwordSwitch && vm.formData.passwordEdit) {
              // 有修改密码 修改函数和原始密码函数替换 删除修改的函数
              vm.formData.password = vm.formData.passwordEdit;
              delete vm.formData.passwordEdit;
            }
            let param = {
              ...this.formData,
            };
            Object.assign(param, { ["status_text"]: undefined });
            if (this.formData.id) {
              schoolUpdate(param).then((res) => {
                this.loadingSave = false;
                if (res.code == 1) {
                  this.$message.success(res.msg);
                  this.handleClose();
                  this.$emit("refreshData");
                } else {
                  this.$message.warning(res.msg);
                }
              });
            } else {
              schoolAdd(param).then((res) => {
                this.loadingSave = false;
                if (res.code == 1) {
                  this.$message.success(res.msg);
                  this.handleClose();
                  this.$emit("refreshData", 1);
                } else {
                  this.$message.warning(res.msg);
                }
              });
            }
          }
        });
      },
      submitData() {
        let vm = this;
        if (vm.passwordSwitch && vm.formData.passwordEdit) {
          vm.$confirm("您修改了改用户的原始密码，您确定要进行密码修改吗?", {
            closeOnClickModal: false,
          })
            .then((res) => {
              vm.submitDataOne();
            })
            .catch(() => {});
        } else {
          vm.submitDataOne();
        }
      },
      imgBeforeUpload(file) {
        const isJPG = file.type === "image/jpeg" || file.type === "image/png";
        if (!isJPG) {
          this.$message.error("图片只能是 JPG/PNG 格式!");
          return false;
        }
        return isJPG;
      },
      // async imgUploadRequest(param) {
      //     var vm = this
      //     let obj= await platformsettingsUploadPlatformImg(param)
      //     if(obj.code == 1) {
      //         let res=''
      //         if (obj.data.data[0].indexOf("://")>=0){
      //             res = obj.data.data[0]
  
      //         }else{
      //             res = url.split('/api')[0]+obj.data.data[0]
      //         }
      //         vm.formData.default_image = res
      //     } else {
      //         vm.$message.warning(res.msg)
      //     }
      // },
      imgUploadSuccess(res) {
        // if (res) {
        //     this.formData.image = res.url
        // }
      },
      getAreaRoot() {
        // 地区列表获取
        let objs = {
          page: 1,
          per_page: 999, //设置最长数据
        };
        addressAreaRoot(objs).then((res) => {
          if (res.code == 1) {
            console.log(res, "数据获取成功------------");
            this.AreaRootList = res.data.data;
          }
        });
      },
    },
    mounted() {
      console.log("开始获取，‘-----------", this.refreshData);
      //this.getAreaRoot();
    },
  };
  </script>
  <style scoped>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 128px;
    height: 128px;
    line-height: 128px;
    text-align: center;
  }
  
  .avatar {
    width: 128px;
    height: 128px;
    display: block;
  }
  </style>
  